package Railroad.GUI;

import Railroad.Communication.CommDriver;
import Railroad.ObsadzovacCiest;
import Railroad.Zoznam;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RailroadPanel extends javax.swing.JPanel
        implements MouseListener, MouseMotionListener
{ 
    public static final int rasterX = 10; // Posun po 10 px vodorovne.
    public static final int rasterY = 15; // POsun po 15 px zvislo.
    public boolean rus=false;
    Zoznam zoznam;
    TrackDraw draw = new TrackDraw();
    ObsadzovacCiest obsadzovac;
    CommDriver communication;
    RiadenieNavestidiel riadenie;
   boolean menu=false;
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(rasterX * 80, rasterY * 40); // Nastavenie veľkosti okna.
    }

    public void setRus(boolean rus) {
        this.rus = rus;
    }
    /**
     * Creates new form RailroadPanel
     */
    @SuppressWarnings("LeakingThisInConstructor")
    public RailroadPanel() {
        initComponents();
        addMouseListener(this); // pridanie listenerov.
        addMouseMotionListener(this);
        setBackground(Color.black); // Nastavenie farby pozadia.
    }
    
    public void setZoznam(Zoznam z) {
        this.zoznam = z;
        draw.setZoznam(z);
    }

    public Zoznam getZoznam() {
        return zoznam;
    }
    public void setObsadzovac(ObsadzovacCiest o)
    {
        this.obsadzovac=o;
    }
    public ObsadzovacCiest getObsadzovac()
    {
        return obsadzovac;
    }

    public void setRiadenie(RiadenieNavestidiel riadenie) {
        this.riadenie = riadenie;
        riadenie.setZ(obsadzovac.getTrackList());
    }

    public RiadenieNavestidiel getRiadenie() {
        return riadenie;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 479, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 332, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

private int mouseX, mouseY; // Súradnice kurzoru.
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public void paint(Graphics g) {
        super.paint(g); // Vykreslenie cierneho pozadia.
        //graph=g;
        drawUpperStrip(g);
        draw.drawBackground(g);
        drawCursor(g);
        draw.drawForeground(g);
        
    }
    
    private void drawCursor(Graphics g) {
        g.setColor(new Color(255,0,255)); // Nastavenie farby kurzora.
        g.fillRect(mouseX * rasterX, mouseY * rasterY, rasterX, rasterY); // Zafarbenie políčka, na ktorom je aktuálne kurzor.
    }
    
    void restoreCursor() {
        Cursor cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR); 
        setCursor(cursor);
    }
    
    void clearCursor() {
        BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
        setCursor(blankCursor);
    }
    
    private void drawUpperStrip(Graphics g) {
        final Color bkColor = new Color(0, 0, 128);
        int width = this.getWidth();
        g.setColor(bkColor);
        g.fillRect(0, 0, width, 2 * rasterY);
        drawDateAndTime(g);
        drawUPMenu(g);
        if(menu){
        drawMenu(g);
        }
    }
    
    private void drawDateAndTime(Graphics g) {
        int windowWidth = getWidth();
        Font font = new Font("Courier new", Font.BOLD, 22);
        g.setFont(font);
        
        final Color textColor = new Color(255, 255, 255);
        final DateFormat dateFormat = new SimpleDateFormat("dd.MM.yy  HH:mm:ss");
        final Date date = new Date();
        final String text = dateFormat.format(date);
        
        FontMetrics fm = getFontMetrics(font);
        int textWidth = fm.stringWidth(text);
        
        g.setColor(textColor);
        g.drawString(text, windowWidth - textWidth, rasterY * 3 / 2);
        
    }
    private void drawUPMenu(Graphics g) {
        
        Font font = new Font("Courier new", Font.BOLD, 22);
        g.setFont(font);
        
        final Color textColor = new Color(255, 255, 255);
        
        final String text = "Menu";
        
        
        
        
        g.setColor(textColor);
        g.drawString(text, 1, rasterY * 3 / 2);
        
    }
    private void drawMenu(Graphics g) {
        
        Font font = new Font("Courier new", Font.BOLD, 22);
        g.setFont(font);
        
        final Color textColor = new Color(255, 255, 255);
       
        final String text = "RC";
        
        
        
        g.setColor(textColor);
        g.drawString(text, 1*rasterX, 3*rasterY);
    }
    
    @Override
    public void mouseEntered(MouseEvent e) {
        clearCursor();
    }
    
    @Override
    public void mouseClicked(MouseEvent e){
        menu=false;
        switch (e.getButton()) {
            
            case MouseEvent.BUTTON1:
               if(mouseX<6 && mouseY<2)
            {
            menu=true;
            }
          
                
               if (rus) {
                    
                    obsadzovac.zrusCestu(mouseX, mouseY);
                   // riadenie.prestavNavestidla();
                    repaint();
                    this.setRus(false);
                    
                    
                } else {
                    obsadzovac.obsadCestu(mouseX, mouseY);
                    //riadenie.prestavNavestidla();///
               
                }
             if(mouseX<6 && mouseY==2)
            {
            rus=true;
            }
            break;
            case MouseEvent.BUTTON3:
                obsadzovac.zrusOznacenie();
                break;
        }
        repaint();
    }
       
    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {
        restoreCursor();
    }

    @Override
    public void mouseDragged(MouseEvent e) {}

    @Override
    public void mouseMoved(MouseEvent e) { //pohyb mysi prepocitany na policka a prekreslenie pri pohybe
        mouseX = e.getX() / rasterX;
        mouseY = e.getY() / rasterY;
        this.repaint();
    }

    void setCommunication(CommDriver communication) {
        this.communication = communication;
    }
}
